package rdbms;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DAO.DatabasePool;
import DAO.ENSequenceDAO;


import model.DIDomain;
import model.ENSequence;

public class RDBMSDIDomainDAO {

	private String idConnection;
	private ENSequenceDAO sequence;

	private static String INSERT = "insert into DI_DOMAIN (DOMAIN_ID, DESCRIPTION) values (?, ?)";

	private static String DELETE = "delete from DI_DOMAIN where DOMAIN_ID = ? ";

	private static String DELETE_ALL = "delete from DI_DOMAIN";

	private static String SELECT_BY_ID = "select description "
			+ "from DI_DOMAIN where DOMAIN_id = ? ";

	private static String SELECT_BY_DESCRIPTION = "select domain_id "
			+ "from DI_DOMAIN where description = ? ";

	private static String SELECT_ALL = "select * from DI_DOMAIN";

	private static String UPDATE = "update set description = ? where domain_id = ? ";

	public RDBMSDIDomainDAO(final String idConnection,
			final ENSequenceDAO sequence) throws SQLException {
		this.idConnection = idConnection;
		this.sequence = sequence;
	}

	public boolean insert(DIDomain diDomain) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			diDomain.setDomainId(sequence.nextval(ENSequence.DOMAIN_ID));
			pst = conn.prepareStatement(INSERT);
			pst.setInt(1, diDomain.getDomainId());
			pst.setString(2, diDomain.getDescription());
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while inserting DOMAIN.", e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println("Dominio Inserido - Commit");
			} else {
				conn.rollback();
				System.out.println("Dominio Nao Inserido - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public boolean delete(int id) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(DELETE);
			pst.setInt(1, id);
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while deleting DOMAIN.", e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println("Dominio Deletado - Commit");
			} else {
				conn.rollback();
				System.out.println("Dominio nao Deletado - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public boolean deleteAll() throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(DELETE_ALL);
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while deleting(all) DOMAIN.", e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println("Dominios Deletados - Commit");
			} else {
				conn.rollback();
				System.out.println("Dominios Nao Deletados - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public DIDomain find(int id) throws Exception {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rss = null;
		DIDomain diDomain = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(SELECT_BY_ID);
			pst.setInt(1, id);
			rss = pst.executeQuery();
			if (rss.next()) {
				diDomain = new DIDomain(id, rss.getString(1));
			}
			return diDomain;
		} catch (Exception e) {
			throw new Exception("Exception while retrieving DIDomain.", e);
		} finally {
			if (rss != null)
				rss.close();
			if (pst != null)
				pst.close();
		}
	}

	public DIDomain find(String description) throws Exception {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rss = null;
		DIDomain diDomain = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(SELECT_BY_DESCRIPTION);
			pst.setString(1, description);
			rss = pst.executeQuery();
			if (rss.next()) {
				diDomain = new DIDomain(rss.getInt(1), description);
			}
			return diDomain;
		} catch (Exception e) {
			throw new Exception("Exception while retrieving DIDomain.", e);
		} finally {
			if (rss != null)
				rss.close();
			if (pst != null)
				pst.close();
		}
	}

	public boolean update(DIDomain diDomain) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(UPDATE);
			pst.setString(1, diDomain.getDescription());
			pst.setInt(2, diDomain.getDomainId());
			pst.execute(); 
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while updating DIDomain.", e);
		} finally {
			if (pst != null) pst.close();
		}
		return success;
	}
	
	public List<DIDomain> list(int noi) throws Exception {
		List<DIDomain> list = null;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rss = null;
		int ctr = 0;
		try {
			conn = DatabasePool.getConnection(idConnection);
			list = new ArrayList<DIDomain>();
			pst = conn.prepareStatement(SELECT_ALL);
			rss = pst.executeQuery();
			while (rss.next()) {
				if (++ctr > noi)
					break;
				DIDomain diDomain = new DIDomain(rss.getInt(1),
						rss.getString(2));
				list.add(diDomain);
			}
			return list;
		} catch (Exception e) {
			throw new Exception("Exception while retrieving DIDomain.", e);
		} finally {
			if (rss != null)
				rss.close();
			if (pst != null)
				pst.close();
		}
	}
}
